let express = require('express')();
let serve = require('express-static');

let mysql = require('mysql');
var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'school'
});

sql.connect();




//处理页面记载时请求的接口
express.get('/sel', function(requst, response) {
    sql.query('SELECT * FROM students', function(error, results) {
        if (error) {
            console.log(error);
            response.send(error)
        } else {
            response.send(results)
        }

    });

})


//处理删除操作的接口
express.get('/del', function(requst, response) {

    sql.query(`DELETE FROM students WHERE id = "${requst.query.id}"`, function(error, results) {
        if (error) {
            console.log(error);
            response.send(error)
        } else {
            response.send(results)
        }

    });

})

//处理添加操作的接口
express.get('/add', function(requst, response) {
    let n = requst.query;
    sql.query(`INSERT INTO students (name,sex,age,city,date) VALUES ("${n.name}","${n.sex}","${n.age}","${n.city}","${n.date}")`, function(error, results) {
        if (error) {
            console.log(error);
            response.send(error)
        } else {
            console.log(results)
            response.send(results)
        }

    });

})

//处理修改操作的接口
express.get('/edit', function(requst, response) {
    let n = requst.query;
    sql.query(`UPDATE students SET name = "${n.name}",sex = "${n.sex}",age = "${n.age}" ,city = "${n.city}",date = "${n.date}" WHERE id = "${n.id}"`, function(error, results) {
        if (error) {
            console.log(error);
            response.send(error)
        } else {
            console.log(results)
            response.send(results)
        }

    });

})






express.use(serve(__dirname + '/studentManage'));
express.listen(8080)